﻿using System;
using System.Text;
using System.IO;
using YXYLCommon;
using System.Collections.Generic;
using System.Data;

namespace Entity
{

    public enum eReportOpenType
    {
        /// <summary>
        /// 预览
        /// </summary>
        Preview = 1,

        /// <summary>
        /// 打印
        /// </summary>
        Print = 2,

        /// <summary>
        /// 设计
        /// </summary>
        Design = 3,
    }

    public class MyReport
    {
        #region Property

        /// <summary>
        /// 报表
        /// </summary>
        public BAOBIAODY BBDY { get; set; }
        /// <summary>
        /// 报表模板
        /// </summary>
        public BaoBiaoMB BBMB { get; set; }
        /// <summary>
        /// 报表模板参数
        /// </summary>
        public BaoBiaoMBCS BBMBCS { get; set; }

        /// <summary>
        /// fastreport对象
        /// </summary>
        //public Report Report;

        /// <summary>
        /// 打印参数
        /// </summary>
        public PrintOptions PrintOptions { get; set; }

        protected List<BaoBiaoCS> csList;

        /// <summary>
        /// 报表打开方式(预览,设计,打印)
        /// </summary>
        public eReportOpenType OpenType;

        protected DALBaoBiaoDY dalDY = new DALBaoBiaoDY();
        protected DALBaoBiaoCS dalCS = new DALBaoBiaoCS();
        protected DALBaoBiaoMB dalMB = new DALBaoBiaoMB();
        protected DALBaoBiaoMBCS dalMBCS = new DALBaoBiaoMBCS();

        #endregion

        #region Contructor

        /// <summary>
        /// 实例化报表
        /// </summary>
        /// <param name="opentype">打开方式(设计,预览,打印)</param>
        /// <param name="csList">获取数据的存储过程参数列表</param>
        /// <param name="printoptions">打印设置(偏移量等)</param>
        public MyReport(eReportOpenType opentype,List<BaoBiaoCS> csList,PrintOptions printoptions)
        {
            this.OpenType = opentype;
            this.csList = csList;
            this.PrintOptions = printoptions;
        }

        #endregion

        #region Method

        /// <summary>
        /// 初始化报表
        /// </summary>
        /// <param name="baobiaoid">报表ID</param>
        /// <param name="zhujimc">主机名称</param>
        /// <param name="zhujiip">主机IP</param>
        /// <returns></returns>
        public  DbCmdResult Init(int baobiaoid, string zhujimc, string zhujiip)
        {
            DbCmdResult rs = new DbCmdResult(true);
            try
            {
                this.BBDY = this.dalDY.GetByBaoBiaoID(baobiaoid);
                if (!this.BBDY.IsExists)
                {
                    rs.IsSuccess = false;
                    rs.ReturnMsg = "报表不存在,请检查报表ID是否正确";
                    return rs;
                }
                this.BBMB = this.dalMB.GetByBaoBiaoId(baobiaoid);
                //不存在该报表模版数据情况下,需指定其报表ID,保存模版时使用.
                this.BBMB.I_BAOBIAOID = this.BBDY.I_BAOBIAOID;
                //未传入打印选项,从数据库加载
                if (this.PrintOptions == null)
                {
                    this.PrintOptions = new PrintOptions();
                    this.BBMBCS = this.dalMBCS.GetByBaoBiaoAndZhuJi(baobiaoid, zhujimc, zhujiip);
                    //数据库中存在,加载
                    if (this.BBMBCS.IsExists)
                    {
                        this.PrintOptions.Copies = DbInt.Parse(this.BBMBCS.I_COPIES);
                        this.PrintOptions.PrinterName = this.BBMBCS.S_PRINTERNAME;
                        this.PrintOptions.Timestamp = this.BBMBCS.S_SHIJIANC;
                        this.PrintOptions.X = (float)this.BBMBCS.I_X.Value;
                        this.PrintOptions.Y = (float)this.BBMBCS.I_Y.Value;
                    }
                    else//不存在,从本地打印机设置加载
                    {
                        this.BBMBCS = new BaoBiaoMBCS();
                        //预置代码块
                        //
                        //                    
                    }
                }
                return rs;
            }
            catch (Exception ex)
            {
                rs.IsSuccess = false;
                rs.ReturnMsg = ex.Message;
                return rs;
            }            
        }

        /// <summary>
        /// 根据报表数据源存储过程与参数获取数据
        /// </summary>
        /// <returns></returns>
        public virtual DbCmdResult LoadData()
        {
            DbCmdResult rs = new DbCmdResult(true);
            if (!string.IsNullOrEmpty(this.BBDY.S_CHAXUNGC))
            {
                //注册数据源
                DataSet ds = this.dalDY.GetDataByCS(this.BBDY, this.csList);
                //if (dt.Rows.Count == 0)
                //{
                //    rs.IsSuccess = false;
                //    rs.ReturnMsg = "当前参数查询结果无数据,请更换参数.";
                //}
                //dt.TableName = "tbData";
                //this.RegisterData(dt);
                this.RegisterData(ds);
            }
            return rs;
        }

        public virtual void LoadXY()
        {
            
        }

        /// <summary>
        /// 报表对象注册数据源
        /// </summary>
        /// <param name="dt">数据源</param>
        protected virtual void RegisterData(DataSet ds)
        { 
        
        }

        /// <summary>
        /// 加载报表模版
        /// </summary>
        public virtual DbCmdResult LoadTemplate()
        {
            DbCmdResult rs = new DbCmdResult(true);
            if (!this.BBMB.IsExists)
            {
                if (this.OpenType != eReportOpenType.Design)
                {
                    rs.IsSuccess = false;
                    rs.ReturnMsg = "未找到模版数据";
                    return rs;
                }
                else
                {
                    return rs;
                }
            }
            else
            {
                LoadTemplateFromData();                
                return rs;
            }
        }

        protected virtual void LoadTemplateFromData()
        { 
        
        }
        protected virtual string SaveTemplateToData()
        {
            return null;
        }

         /// <summary>
        /// 加载报表模版
        /// </summary>
        public virtual DbCmdResult Open()
        {
            DbCmdResult rs = new DbCmdResult(true);
            if (this.OpenType == eReportOpenType.Design)
            {
                this.Design();
                if (this.BBMB.IsExists)
                {
                    this.BBMB.Operate = eModelOperate.Update;
                }
                else
                {
                    this.BBMB.Operate = eModelOperate.Add;
                }
                string str = this.SaveTemplateToData();
                this.BBMB.S_MUBAN = Encoding.Default.GetBytes(str);
                this.dalMB.Save(this.BBMB);
                return rs;
            }
            else if (this.OpenType == eReportOpenType.Preview)
            {
                this.SetOffset();
                this.Show();
            }
            else if (this.OpenType == eReportOpenType.Print)
            {
                this.SetOffset();
                this.Print();
            }
            return rs;
        }

        protected virtual void Design()
        { }
        protected virtual void Show()
        { }
        protected virtual void Print()
        { }

        /// <summary>
        /// 设置待打印模板各控件的偏移
        /// </summary>
        protected virtual void SetOffset()
        {
           
        }

        public virtual void Dispose()
        { }

        #endregion
    }


}
